package com.lzs.kucun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzs.entity.*;
import com.lzs.kucun.mapper.StockMapper;
import com.lzs.kucun.service.StockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.lzs.util.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wpy
 * @since 2020-11-25
 */
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements StockService {
    @Autowired
    private  StockMapper stockMapper;

    @Override
    public Page<Stock> selectStockPage(Integer current, Integer size, Stock stock,String[] warehouseid) {
        Page<Stock> stockPage=new Page<>(current,size);
        String commoditybar = stock.getCommoditybar();
        String commodityname = stock.getCommodityname();
        String locatorcode = stock.getLocatorcode();
        String locatorname = stock.getLocatorname();
        String state = stock.getLocatorstate();
        QueryWrapper<Stock> queryWrapper=new QueryWrapper<>();
        queryWrapper.in("warehouseid",warehouseid);
        if (StringUtils.isNotEmpty(commoditybar)){
            queryWrapper.like("commoditybar",commoditybar);
        }
        if (StringUtils.isNotEmpty(commodityname)){
            queryWrapper.like("commodityname",commodityname);
        }
        if (StringUtils.isNotEmpty(locatorcode)){
            queryWrapper.like("locatorcode",locatorcode);
        }
        if (StringUtils.isNotEmpty(locatorname)){
            queryWrapper.like("locatorname",locatorname);
        }
        if (StringUtils.isNotEmpty(state)){
            queryWrapper.like("state",state);
        }
        stockMapper.selectPage(stockPage,queryWrapper);
        return stockPage;
    }


    @Override
    public R updateStockQuantity(Long locatorcode,Long commodityid,Long warehouseid,Integer qty) {
        QueryWrapper<Stock> stockQueryWrapper=new QueryWrapper<>();
        stockQueryWrapper.eq("warehouseid",warehouseid);
        stockQueryWrapper.eq("commodityid",commodityid);
        stockQueryWrapper.eq("locatorid",locatorcode);
        List<Stock> stocks = stockMapper.selectList(stockQueryWrapper);
        Integer uqty = stocks.get(0).getUqty();
        if (qty>uqty){
            return R.error().message("库存数量不足");
        }else {
            stockMapper.updateStockQuantity(qty, warehouseid, commodityid,locatorcode);
            return R.ok().message("扣减库存成功");
        }
    }


    @Override// c- l-
    public R updateLqtyQuantity(String locatorcode,String commoditybar,Long warehouseid,Integer qty) {
        QueryWrapper<Stock> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("locatorcode",locatorcode);
        queryWrapper.eq("commoditybar",commoditybar);
        queryWrapper.eq("warehouseid",warehouseid);
        Stock stock = stockMapper.selectList(queryWrapper).get(0);
        Integer uqty = stock.getUqty();
        Integer cqty = stock.getCqty();
        Integer lqty = stock.getLqty();
        Stock stock1 = new Stock();
        stock1.setCqty(cqty-qty);
        stock1.setLqty(lqty-qty);

        stockMapper.update(stock1,queryWrapper);
        return R.ok();
    }


    @Override
    public R selectCommoditybar(String locatorcode, Long wid) {
        try {

            QueryWrapper<Stock> queryWrapper = new QueryWrapper<>();
            if (StringUtils.isNotEmpty(locatorcode)) {
                queryWrapper.eq("locatorcode", locatorcode);
            }
            if (wid != null && wid + "" != "") {
                queryWrapper.eq("warehouseid", wid);
            }
            List<Stock> stocks = stockMapper.selectList(queryWrapper);
            return R.ok().message("查询商品编码成功").data("commoditybars", stocks);
        }catch (RuntimeException e){
            e.printStackTrace();
            return R.error().message("查询商品编码失败");
        }

    }

    @Override
    public Stock selectQuantity(Long wid, String ccode, String lcode) {
        QueryWrapper<Stock> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("locatorcode",lcode);
        queryWrapper.eq("commoditybar",ccode);
        queryWrapper.eq("warehouseid",wid);
        return stockMapper.selectList(queryWrapper).get(0);
    }

    @Override// l+ u-
    public R updateUqtyQuantity(String locatorcode, String commoditybar, Long warehouseid, Integer qty)throws RuntimeException{
        QueryWrapper<Stock> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("locatorcode",locatorcode);
        queryWrapper.eq("commoditybar",commoditybar);
        queryWrapper.eq("warehouseid",warehouseid);
        Stock stock = stockMapper.selectList(queryWrapper).get(0);
        Integer uqty = stock.getUqty();
        Integer cqty = stock.getCqty();
        Integer lqty = stock.getLqty();
        Stock stock1 = new Stock();
        if (qty>uqty){
            return R.error();
        }
        stock1.setLqty(lqty+qty);
        stock1.setUqty(uqty-qty);
        stockMapper.update(stock1,queryWrapper);
        return R.ok();
    }

    @Override// l+ c+
    public R updateLqtyCQuantity(String locatorcode, String commoditybar, Long warehouseid, Integer qty)throws RuntimeException{
        QueryWrapper<Stock> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("locatorcode",locatorcode);
        queryWrapper.eq("commoditybar",commoditybar);
        queryWrapper.eq("warehouseid",warehouseid);
        Stock stock = stockMapper.selectList(queryWrapper).get(0);
        Integer uqty = stock.getUqty();
        Integer cqty = stock.getCqty();
        Integer lqty = stock.getLqty();
        Stock stock1 = new Stock();
        stock1.setLqty(lqty+qty);
        stock1.setCqty(cqty+qty);
        stockMapper.update(stock1,queryWrapper);
        return R.ok();
    }

    @Override
    public User selectUser(Long userid) {
        return stockMapper.selectUser(userid);
    }

    @Override
    public Locator selectLocator(String locatorcode) {
        return stockMapper.selectLocator(locatorcode);
    }

    @Override
    public Commodity selectCommodity(String commoditycode) {
        return stockMapper.selectCommodity(commoditycode);
    }

    @Override
    public Warehouse selectWarehouse(Long warehouseid) {
        return stockMapper.selectWarehouse(warehouseid);
    }


    @Override
    public Boolean selectIsExit(String locatorcode, String commoditybar, Long warehouseid) {
        QueryWrapper<Stock> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("locatorcode",locatorcode);
        queryWrapper.eq("commoditybar",commoditybar);
        queryWrapper.eq("warehouseid",warehouseid);
        List<Stock> stocks = stockMapper.selectList(queryWrapper);
        if (stocks!=null&&stocks.size()>0){
            return true;
        }else {
        return false;
        }
    }


    @Override// c+ u+
    public R updateAllQuantity(String locatorcode, String commoditybar, Long warehouseid, Integer qty) {
        QueryWrapper<Stock> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("locatorcode",locatorcode);
        queryWrapper.eq("commoditybar",commoditybar);
        queryWrapper.eq("warehouseid",warehouseid);
        Stock stock = stockMapper.selectList(queryWrapper).get(0);
        Integer uqty = stock.getUqty();
        Integer cqty = stock.getCqty();
        Integer lqty = stock.getLqty();
        Stock stock1 = new Stock();
        stock1.setCqty(cqty+qty);
        stock1.setUqty(uqty+qty);
        stockMapper.update(stock1,queryWrapper);
        return R.ok();
    }

    @Override//c- u-
    public R reduceQuantity(String locatorcode, String commoditybar, Long warehouseid, Integer qty) {
        QueryWrapper<Stock> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("locatorcode",locatorcode);
        queryWrapper.eq("commoditybar",commoditybar);
        queryWrapper.eq("warehouseid",warehouseid);
        Stock stock = stockMapper.selectList(queryWrapper).get(0);
        Integer uqty = stock.getUqty();
        Integer cqty = stock.getCqty();
        Integer lqty = stock.getLqty();
        if (qty>uqty){
            return R.error();
        }
        Stock stock1 = new Stock();
        stock1.setCqty(cqty-qty);
        stock1.setUqty(uqty-qty);
        stockMapper.update(stock1,queryWrapper);
        return R.ok();
    }

    @Override
    public List<Stock> selectStockInfo(Long warehouseid, String type) {
        return stockMapper.selectStockInfo(warehouseid,type);
    }


}
